/*
 * Copyright [2009] [Marcin Rzeźnicki]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
package mr.go.yaxc.actions;

import mr.go.yaxc.Action;
import mr.go.yaxc.ConfigurationError;
import mr.go.yaxc.ScopeHandler;

import org.xml.sax.Attributes;

/**
 * This action creates a new instance of <i>class</i>
 * 
 * <h6>on xml element start:</h6> Creates new instance of <i>class</i> using
 * default constructor and sets it as <i>object</i> in current scope <h6>on xml
 * element end:</h6> passes <i>object</i> to parent's <i>params</i>, or, if
 * <i>id</i> attribute is set, puts it in the global context <h6>prerequisites:</h6>
 * none <h6>attributes:</h6>
 * <table>
 * <tr>
 * <th>Attribute name</th>
 * <th>What is it for?</th>
 * <th>Required?</th>
 * </tr>
 * <tr>
 * <td><i>class</i></td>
 * <td>qualified classname of instance to be created</td>
 * <td>yes</td>
 * </tr>
 * <tr>
 * <td><i>id</i></td>
 * <td>if set, puts new instance in the global context</td>
 * <td>no</td>
 * </tr>
 * </table>
 *<p>
 * Example:<br>
 * 
 * <pre>
 * &lt;New class=&quot;mr.go.yaxc.tests.test1.Bean&quot; id=&quot;bean&quot;&gt;
 * 		&lt;Properties&gt;
 * 			&lt;intValue&gt;5&lt;/intValue&gt;
 * 			&lt;stringValue&gt;abc&lt;/stringValue&gt;
 * 			&lt;booleanValue&gt;true&lt;/booleanValue&gt;
 * 			&lt;doubleValue&gt;0.5&lt;/doubleValue&gt;
 * 			&lt;floatValue&gt;1.25&lt;/floatValue&gt;
 * 		&lt;/Properties&gt;
 * 	&lt;/New&gt;
 * </pre>
 * 
 * </p>
 * 
 * 
 * @author Marcin Rzeźnicki
 * 
 */
public class New extends Action {

    private String id;

    @Override
    public void carryOut(String name, Attributes attributes,
                         ScopeHandler handler)
            throws Exception {
        String classname = attributes.getValue(getAttribute("class"));
        if (classname == null) {
            throw new ConfigurationError("Missing attribute: " + getAttribute(
                    "class"));
        }
        id = attributes.getValue(getAttribute("id"));
        object = createInstance(classname);
        if (handler.getLogger().isDebugEnabled()) {
            handler.getLogger().debug("New: {}",
                                      object.toString());
        }
    }

    @Override
    public void processed(String body, ScopeHandler handler)
            throws Exception {
        if (id != null) {
            handler.setReference(id,
                                 object);
            if (handler.getLogger().isDebugEnabled()) {
                handler.getLogger().debug("Id '{}': {}",
                                          id,
                                          object.toString());
            }
        }
        handler.tryAddParam(object);
    }
}
